package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.nailcampus.acl.mapper.*;
import com.hua.nailcampus.acl.model.*;
import com.hua.nailcampus.acl.model.vo.ApprovalVo;
import com.hua.nailcampus.acl.service.ApprovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:chenjianhua
 * @Date:2023/12/1 22:24
 * @Version 17
 */
@Slf4j
@Service
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper,Approval>
        implements ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private UserSchoolMapper userSchoolMapper;
    @Autowired
    private AvatarMapper avatarMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Override
    public void saveApproval(Approval approval) {
        approvalMapper.insert(approval);
    }

    @Override
    public void updateApproval(Approval approval) {
        approvalMapper.updateById(approval);
    }

    @Override
    public List<Approval> getApproval(Role role) {
//        初始化表单
        List<Approval> approvalList =null;
//        若角色为普通学生,返回自己申请过的表单
        if(role.getCode().equals(0)){
            approvalList = approvalMapper.selectList(
                    new LambdaQueryWrapper<Approval>()
                            .eq(Approval::getUserId, role.getUserId()));
        }
//        若角色为教师，则返回审批者为自己的表单
        if(role.getCode().equals(1)){
            approvalList = approvalMapper.selectList(
                    new LambdaQueryWrapper<Approval>()
                            .eq(Approval::getSecondStepUserId, role.getUserId()));
        }
//        若角色为辅导员,则返回审批表为自己的表单
        if(role.getCode().equals(2)){
            approvalList = approvalMapper.selectList(
                    new LambdaQueryWrapper<Approval>()
                            .eq(Approval::getThirdStepUserId,role.getUserId()));
        }
        return approvalList;
    }

    @Override
    public List<ApprovalVo> getUnProcessApproval(Integer userId,Role role) {
//        创建集合对象，不开辟对象空间
//        若角色为普通学生，则直接返回空
        List<Approval> approvalList = new ArrayList<>();
        if(role.getCode().equals(0)){
            return null;
        }
//        若角色为教师，则直接返回进度为1
        if(role.getCode().equals(1)){
            List<Approval> approvalList1 = approvalMapper.selectList(
                    new LambdaQueryWrapper<Approval>()
                            .eq(Approval::getStep, 1)
            );
            approvalList = this.getSchoolApprovalList(userId,approvalList1);
        }
//        若角色为辅导员，则返回进度为2，且Result为0的审批表集合
        if(role.getCode().equals(2)){
            List<Approval> approvalList1 = approvalMapper.selectList(
                    new LambdaQueryWrapper<Approval>()
                            .eq(Approval::getStep, 2)
                            .eq(Approval::getResult, 0));
            approvalList = this.getSchoolApprovalList(userId,approvalList1);
        }
        List<ApprovalVo> approvalVoList = new ArrayList<>();
        approvalVoList = this.getApprovalVoList(approvalList);
        return approvalVoList;
    }

    @Override
    public List<ApprovalVo> getUserApproval(Integer userId,Integer roleId) {
        List<Approval> approvalList = null;
        if(roleId.equals(0)){
            approvalList = approvalMapper.selectList(new LambdaQueryWrapper<Approval>()
                    .eq(Approval::getUserId, userId));
        }
        if(roleId.equals(1)){
            List<Approval> approvalList1 = approvalMapper.selectList(new LambdaQueryWrapper<Approval>()
                    .eq(Approval::getSecondStepUserId, userId)
                    .or()
                    .eq(Approval::getStep,1));
            approvalList = this.getSchoolApprovalList(userId,approvalList1);
        }
        if(roleId.equals(2)){
            List<Approval>  approvalList1 = approvalMapper.selectList(new LambdaQueryWrapper<Approval>()
                    .eq(Approval::getThirdStepUserId, userId)
                    .or()
                    .eq(Approval::getStep,2));
            approvalList = this.getSchoolApprovalList(userId,approvalList1);
        }
        List<ApprovalVo> approvalVoList = this.getApprovalVoList(approvalList);
        return approvalVoList;
    }

    @Override
    public ApprovalVo getApprovalById(Integer id) {
        Approval approval = approvalMapper.selectById(id);
        ApprovalVo approvalVo = new ApprovalVo(approval);
        UserSchool userSchool = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                .eq(UserSchool::getUserId, approval.getUserId()));
        approvalVo.setUserName(userSchool.getRealName());
        Avatar avatar = avatarMapper.selectOne(new LambdaQueryWrapper<Avatar>().eq(Avatar::getUserId, approval.getUserId()));
        if(avatar!=null){
            approvalVo.setAvaUrl(avatar.getUrl());
        }
//        设置审批教师姓名
        if(approval.getSecondStepUserId()!=null){
            UserSchool userSchool1 = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>().eq(UserSchool::getUserId, approval.getSecondStepUserId()));
            approvalVo.setSecondStepUserName(userSchool1.getRealName());
        }
//        设置审批辅导员姓名
        if(approval.getThirdStepUserId()!=null){
            UserSchool userSchool1 = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>().eq(UserSchool::getUserId, approval.getThirdStepUserId()));
            approvalVo.setThirdStepUserName(userSchool1.getRealName());
        }
//            设置家庭地址省份以及城市
        City homeCity = cityMapper.selectById(approval.getHomeCityId());
        Province homeProvince = provinceMapper.selectById(homeCity.getPId());
        approvalVo.setHomeCityName(homeCity.getName());
        approvalVo.setHomeProvinceName(homeProvince.getName());
//            设置目的地省份以及城市
        City arrivalCity = cityMapper.selectById(approval.getArrivalCityId());
        Province arrivalProvince = provinceMapper.selectById(arrivalCity.getPId());
        approvalVo.setArrivalCityName(arrivalCity.getName());
        approvalVo.setArrivalProvinceName(arrivalProvince.getName());
        return approvalVo;
    }


    public List<ApprovalVo> getApprovalVoList(List<Approval> approvalList){
        List<ApprovalVo> approvalVoList = new ArrayList<>();
        for (Approval approval : approvalList){
            ApprovalVo approvalVo = new ApprovalVo(approval);
            UserSchool userSchool = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                    .eq(UserSchool::getUserId, approval.getUserId()));
            approvalVo.setUserName(userSchool.getRealName());
            Avatar avatar = avatarMapper.selectOne(new LambdaQueryWrapper<Avatar>().eq(Avatar::getUserId, approval.getUserId()));
            if(avatar!=null){
                approvalVo.setAvaUrl(avatar.getUrl());
            }
//            设置家庭地址省份以及城市
            City homeCity = cityMapper.selectById(approval.getHomeCityId());
            Province homeProvince = provinceMapper.selectById(homeCity.getPId());
            approvalVo.setHomeCityName(homeCity.getName());
            approvalVo.setHomeProvinceName(homeProvince.getName());
            //        设置审批教师姓名
            if(approval.getSecondStepUserId()!=null){
                UserSchool userSchool1 = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>().eq(UserSchool::getUserId, approval.getSecondStepUserId()));
                approvalVo.setSecondStepUserName(userSchool1.getRealName());
            }
//        设置审批辅导员姓名
            if(approval.getThirdStepUserId()!=null){
                UserSchool userSchool1 = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>().eq(UserSchool::getUserId, approval.getThirdStepUserId()));
                approvalVo.setThirdStepUserName(userSchool1.getRealName());
            }
//            设置目的地省份以及城市
            City arrivalCity = cityMapper.selectById(approval.getArrivalCityId());
            Province arrivalProvince = provinceMapper.selectById(arrivalCity.getPId());
            approvalVo.setArrivalCityName(arrivalCity.getName());
            approvalVo.setArrivalProvinceName(arrivalProvince.getName());
            approvalVoList.add(approvalVo);
        }
        return approvalVoList;
    }

    public List<Approval> getSchoolApprovalList(Integer userId,List<Approval> approvalList){
        List<Approval> newApprovalList = new ArrayList<>();
        for (Approval approval : approvalList){
//                申请者的学校ID
                UserSchool userSchool = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                        .eq(UserSchool::getUserId, approval.getUserId()));
//                该教师的学校ID
                UserSchool userSchool1 = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                        .eq(UserSchool::getUserId, userId));
                if(userSchool.getSchoolId().equals(userSchool1.getSchoolId())){
                    newApprovalList.add(approval);
                }
        }
        return newApprovalList;
    }
}
